<template>
  <div>
    <el-form
      :rules="ruleForm"
      :model="formAccount"
      ref="formRef"
      class="demo-form-inline"
      label-position="right"
      label-width="120px"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="Account" prop="loginName">
            <el-input
              v-model="formAccount.loginName"
              clearable
              :disabled="mode === 'Detail'"
              maxlength="8"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8"
          ><el-form-item label="Name" prop="userName">
            <el-input
              v-model="formAccount.userName"
              :disabled="mode === 'Detail'"
              clearable
              maxlength="8"
              placeholder="请输入"
            /> </el-form-item
        ></el-col>
        <el-col :span="8"
          ><el-form-item label="Password" prop="password">
            <el-input
              v-model="formAccount.password"
              clearable
              :disabled="mode === 'Detail'"
              type="password"
              maxlength="8"
              :placeholder="mode === 'Detail' ? '********' : '请输入'"
            /> </el-form-item
        ></el-col>
      </el-row>
      <el-row>
        <el-col :span="8"
          ><el-form-item label="Role" prop="roleId">
            <el-select
              v-model="formAccount.roleId"
              style="width: 100%"
              :disabled="mode === 'Detail'"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in roleOptions"
                :label="item.roleName"
                :value="item.roleId"
                :key="item.roleId"
              /> </el-select></el-form-item
        ></el-col>
        <el-col :span="16"
          ><el-form-item label="Description" prop="description">
            <el-input
              v-model="formAccount.description"
              clearable
              :disabled="mode === 'Detail'"
              maxlength="8"
              placeholder="请输入"
            /> </el-form-item
        ></el-col>
      </el-row>
    </el-form>
    <div v-if="mode === 'Detail'" class="bottomBtn">
      <el-button @click="handleBack()">Back</el-button>
    </div>
    <div v-else class="bottomBtn">
      <el-button type="primary" color="#3A62D7" @click="save()">Save</el-button>
      <el-button @click="handleBack()">Cancel</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { addAccount } from '@/api/account'
import { getRoleList } from '@/api/role'
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()

const mode = ref()

onMounted(() => {
  console.log('query:', route.query)
  if(route.name.includes('Detail')){
    mode.value = 'Detail'
  }
  formAccount.loginName = route.query.loginName || localStorage.getItem('loginName')
  formAccount.userName = route.query.userName || localStorage.getItem('userName')
  formAccount.roleId = Number(route.query.roleId) ||  Number(localStorage.getItem('accountRoleId'))
  formAccount.description = route.query.description || localStorage.getItem('description')
  formAccount.password = route.query.password || localStorage.getItem('password')
  formAccount.userId = route.query.userId || localStorage.getItem('userId')
  getRolesList()
})
let roleOptions = ref([])
const formRef = ref()
let formAccount = reactive({
  loginName: '',
  userName: '',
  password: '',
  roleId: '',
  description: '',
  userId: ''
})
const ruleForm = reactive({
  loginName: [{ required: true, message: '请输入', trigger: 'blur' }],
  userName: [{ required: true, message: '请输入', trigger: 'blur' }],
  password: [{ required: true, message: '请输入', trigger: 'blur' }]
})
const getRolesList = async () => {
  const res = await getRoleList({
    pageNo: 1,
    pageSize: 999
  })
  if(res.data.code === '0'){
    roleOptions.value = res.data.data.records
  }
}

const save = async () => {
  console.log('form',formAccount);
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await addAccount({
        loginName: formAccount.loginName,
        userName: formAccount.userName,
        password: formAccount.password,
        roleIds: [formAccount.roleId],
        description: formAccount.description,
        userId: formAccount.userId
      })
      if (res.data.code === '0') {
        ElMessage.success(res.data.msg)
        router.back()
      } else {
        ElMessage.error(res.data.msg)
      }
    }
  })
  // console.log(formAccount);
  // console.log( '@@@',res);
}
const handleBack = () => {
  router.back()
}
</script>

<style scoped lang="scss">
::v-deep .demo-form-inline {
  font-weight: 700;
}
::v-deep .el-form-item__label {
  color: #101010;
}
.bottomBtn {
  display: flex;
  justify-content: end;
  margin-top: 26px;
}
::v-deep .el-button {
  width: 76px;
  margin-left: 46px;
}
</style>
